package com.example.book_libapp;

import android.app.Activity;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn_login;
	private Button btn_register;
	private Button btn_passforg;
	private Button btn_home;
	private Button btn_booklistv;
	private Button btn_booksearch;
	private RecordsDbHelper mDbHelper;
	ListActivity listActivitys;
	static final String TAG_KEY = "query";
	
	private static String api_key = "nmc-android";
	CheckConnectServer checkserver;
	private static String RegisterURL = "http://bookstore-sf14.l:8080/api/"
			+ api_key + "";

	public void onSearch(View v) {
		onSearchRequested();
	}

	public void onHome(View v) {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mDbHelper = new RecordsDbHelper(this);
		btn_login = (Button) findViewById(R.id.BtnLogin);
		btn_login.setOnClickListener(this);
		btn_home = (Button) findViewById(R.id.HomePage);
		btn_home.setOnClickListener(this);
		btn_booklistv = (Button) findViewById(R.id.BooklistView);
		btn_booklistv.setOnClickListener(this);
		btn_booksearch = (Button) findViewById(R.id.button1);
		btn_booksearch.setOnClickListener(this);

		checkserver = new CheckConnectServer();

		Intent intent = getIntent();

		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			Log.e("search", "444444444444444444444444444444");
			String query = intent.getStringExtra(SearchManager.QUERY);
			showResults(query);
		} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
			Log.e("search", "33333333333333333333333333333333333");
			Intent recordIntent = new Intent(this, RecordActivity.class);
			recordIntent.setData(intent.getData());
			startActivity(recordIntent);
			finish();
		}

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.layout.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.menu_bookmark:
			// Single menu item is selected do something
			// Ex: launching new activity/screen or show alert message
			Intent i = new Intent(this, Bookmark.class);
			startActivity(i);
			return true;
		case R.id.menu_save:
			Toast.makeText(MainActivity.this, "Save is Selected",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menu_search:
			Intent i2 = new Intent(this, Booksearch.class);
			startActivity(i2);
			return true;
		case R.id.menu_share:
			Intent i1 = new Intent(this, BookHistory.class);
			startActivity(i1);
			return true;
		case R.id.menu_delete:
			Toast.makeText(MainActivity.this, "Delete is Selected",
					Toast.LENGTH_SHORT).show();
			return true;
		case R.id.menu_preferences:
			Toast.makeText(MainActivity.this, "Preferences is Selected",
					Toast.LENGTH_SHORT).show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void showResults(String query) {

		Log.e("search", "55555555555555555555555555");
		Cursor cursor = managedQuery(SuggestionProvider.CONTENT_URI, null,
				null, new String[] { query }, null);
		if (cursor == null) {
			Intent i = new Intent(getApplicationContext(),
					Booksearchresults.class);
			i.putExtra(TAG_KEY, query);
			startActivity(i);
		} else {

			String[] from = new String[] { RecordsDbHelper.KEY_DATA };

			// int[] to = new int[] { R.id.text1 };
			// SimpleCursorAdapter records = new SimpleCursorAdapter(this,
			// R.layout.record, cursor, from, to);
			// listActivitys.getListView().setAdapter(records);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		// if (checkserver.isConnectedToServer(RegisterURL, 3000)) {
		if (v == btn_login) {
			Intent i = new Intent(this, Login.class);
			startActivity(i);
		} else if (v == btn_home) {
			Intent i = new Intent(this, BookHistory.class);
			startActivity(i);
		} else if (v == btn_booklistv) {
			Intent i = new Intent(this, ListViewBookActivity.class);
			startActivity(i);
		} else if (v == btn_booksearch) {
			Intent i = new Intent(this, Booksearch.class);
			startActivity(i);
		}
		// }
		// else {
		// Toast.makeText(getApplicationContext(),
		// "Сервэртэй холбогдохгүй байна!", Toast.LENGTH_LONG).show();
		// }
	}
}